﻿using HtmlAgilityPack;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows8Accelerator.Core.Xaml.Controls.RichText;
using Windows8Accelerator.Core.Xaml.Helpers;

namespace Windows8Accelerator.Core.Xaml.Converters
{
    public class StringToBlocksConverter : DependencyObject
    {
        public static string GetHtmlSource(RichTextBlock richTextBlock)
        {
            return (string)richTextBlock.GetValue(HtmlSourceProperty);
        }

        public static void SetHtmlSource(RichTextBlock richTextBlock, string value)
        {
            richTextBlock.SetValue(HtmlSourceProperty, value);
        }

        public static readonly DependencyProperty HtmlSourceProperty =
            DependencyProperty.Register("HtmlSource", typeof(string), typeof(StringToBlocksConverter), new PropertyMetadata("", PropertyChangedCallback));

        private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is RichTextBlock && e.NewValue is string)
            {
                var loader = new RichTextBlockHtmlLoader();
                loader.LoadAsync(d as RichTextBlock, e.NewValue as string);
            }
        }

        public static ParagraphHtmlSource GetParagraphHtmlSource(Paragraph paragraph)
        {
            return (ParagraphHtmlSource)paragraph.GetValue(ParagraphHtmlSourceProperty);
        }

        public static void SetParagraphHtmlSource(Paragraph paragraph, ParagraphHtmlSource value)
        {
            paragraph.SetValue(ParagraphHtmlSourceProperty, value);
        }

        public static readonly DependencyProperty ParagraphHtmlSourceProperty =
            DependencyProperty.Register("ParagraphHtmlSource", typeof(ParagraphHtmlSource), typeof(StringToBlocksConverter), new PropertyMetadata("", ParagraphHtmlSourcePropertyChangedCallback));

        private static void ParagraphHtmlSourcePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is Paragraph && e.NewValue is ParagraphHtmlSource)
            {
                var paragraph = d as Paragraph;
                var paragraphHtmlSource = e.NewValue as ParagraphHtmlSource;
                paragraphHtmlSource.PropertyChanged += (s1, e1) =>
                {
                    if (paragraphHtmlSource.RichTextBlock != null)
                    {
                        var loader = new RichTextBlockHtmlLoader();
                        loader.LoadAsync(paragraphHtmlSource.RichTextBlock, paragraphHtmlSource.HtmlSource);
                    }
                };
            }
        }

    }
}
